*
* Table 2: Applicant-level summary statistics
*
* Input table: 
*	"${data_applicant_level_tidy}"
*
* Output file: 
*	/results/Table2.tex
*

use "${data_applicant_level_tidy}", clear

* Restrict to the eligible sample
keep if eligibility == 1 & any_ba_listed == 1 & no_funded_version_at_all == 0

keep female age sec_school voc_school capital countycapital town village GPA11 ///
	GPA11_std GPA11_missing disadv sample_hss contract_num contract_num_data ///
	program_num_data ROL_state_funded_only ROL_state_self_funded ROL_self_funded_only

cap file close sumstat
file open sumstat using "${result_dir}/Table2.tex", write replace
file write sumstat "\begin{tabular}{l c c} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. dev.}  \\ " _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)}  \\ \hline" _n

foreach var of varlist female age {
		local lab_var: variable label `var'
	
		qui sum `var'
		local mean_`var': display %4.2f r(mean)
		local sd_`var': display %4.2f r(sd)
		
		file write sumstat " `lab_var' & `mean_`var'' &  (`sd_`var'') \\ " _n

}
file write sumstat "High school & & \\ " _n
foreach var of varlist sec_school voc_school {
		local lab_var: variable label `var'
	
		qui sum `var'
		local mean_`var': display %4.2f r(mean)
		local sd_`var': display %4.2f r(sd)
		
		file write sumstat " `lab_var' & `mean_`var'' &  (`sd_`var'') \\ " _n

}	
file write sumstat "Residence & & \\ " _n
foreach var of varlist capital countycapital town village GPA11 GPA11_std GPA11_missing disadv sample_hss contract_num contract_num_data program_num_data {
		local lab_var: variable label `var'
	
		qui sum `var'
		local mean_`var': display %4.2f r(mean)
		local sd_`var': display %4.2f r(sd)
		
		file write sumstat " `lab_var' & `mean_`var'' &  (`sd_`var'') \\ " _n

}
file write sumstat "ROL by funding type & & \\ " _n
foreach var of varlist ROL_state_funded_only ROL_state_self_funded ROL_self_funded_only {
	local lab_var: variable label `var'
	
	qui sum `var'
	local mean_`var': display %4.2f r(mean)
	local sd_`var': display %4.2f r(sd)
	
	file write sumstat " `lab_var' & `mean_`var'' &  (`sd_`var'') \\ " _n
}

count
local n_obs: display r(N) %6.0gc

file write sumstat "Number of applicants & `n_obs' &  \\ \hline\hline" _n
file write sumstat "\end{tabular}" _n
file close sumstat

* Table notes:
qui sum GPA11_missing
local GPA11_missing_mean = round(r(mean)*100)
di "As a result, 11th-grade GPA is missing for `GPA11_missing_mean' percent of applicants." 
